package com.company.cloud.page.modules.mgr.service.impl;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.modules.cache.utils.PageCachekeys;
import com.company.cloud.page.modules.mgr.controller.req.PageCardSaveParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.page.constant.PageCardTypeEnum;
import com.company.cloud.page.modules.base.entity.PageCardConfigInfo;
import com.company.cloud.page.modules.base.service.PageCardConfigInfoService;
import com.company.cloud.page.modules.mgr.service.PageCardConfigMgrService;

@Service
public class PageCardConfigMgrServiceImpl implements PageCardConfigMgrService {

	@Autowired
	private PageCardConfigInfoService pageCardConfigInfoService;


	@Override
	public PageCardConfigInfo addOrUpdate(PageCardSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		PageCardConfigInfo entity = null;

		Integer userId=null;
		if (PageCardTypeEnum.CardType_Cust.getCode().equals(params.getPageType())) {
			if (params.getUserId()==null || params.getUserId()<=0) {
				userId = userInfo.getId();
			} else {
				userId = params.getUserId();
			}
			entity = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
					.eq("user_id",userId)
					.eq("page_type", PageCardTypeEnum.CardType_Cust.getCode()));
		}


		if (PageCardTypeEnum.CardType_Com.getCode().equals(params.getPageType())) {
			entity = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
					.eq("company_no",userInfo.getCompanyNo())
					.eq("page_type", PageCardTypeEnum.CardType_Com.getCode()));
		}

		if (PageCardTypeEnum.CardType_PT.getCode().equals(params.getPageType())) {
			entity = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
					.eq("page_type", PageCardTypeEnum.CardType_PT.getCode()));
		}


		if (entity == null) {
			entity = new PageCardConfigInfo();
			entity.setPageNo(PageCachekeys.getPageNo());
			entity.setCompanyNo(userInfo.getCompanyNo());
			if (PageCardTypeEnum.CardType_Cust.getCode().equals(params.getPageType())) {
				entity.setPageType(params.getPageType());
				entity.setUserId(userId);
			}else{
				entity.setPageType(params.getPageType());
			}
			entity.setPageName(params.getPageName());
			entity.setPageRemark(params.getPageRemark());
			entity.setContent(params.getContent());
			entity.setIsDraft(params.getIsDraft());
			entity.setShareStyle(params.getShareStyle());
			entity.setShareTitle(params.getShareTitle());
			entity.setShareMemo(params.getShareMemo());
			entity.setShareImg(params.getShareImg());
			entity.addCreateParam(userInfo);
			entity.addEditParam(userInfo);
			pageCardConfigInfoService.save(entity);
		} else {
			entity.setPageName(params.getPageName());
			entity.setPageRemark(params.getPageRemark());
			entity.setContent(params.getContent());
			entity.setIsDraft(params.getIsDraft());
			entity.setShareStyle(params.getShareStyle());
			entity.setShareTitle(params.getShareTitle());
			entity.setShareMemo(params.getShareMemo());
			entity.setShareImg(params.getShareImg());
			entity.setRowsState(EnabledEnum.ENABLED.getCode());
			entity.addEditParam(userInfo);
			pageCardConfigInfoService.updateById(entity);
		}
		return entity;
	}

	@Override
	public PageCardConfigInfo getConfigByUser(LoginSimpleUserInfo userInfo) {
		PageCardConfigInfo entity = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
				.eq("user_id", userInfo.getId())
				.eq("page_type", PageCardTypeEnum.CardType_Cust.getCode())
				.eq("rows_state", EnabledEnum.ENABLED.getCode()));
		if (entity != null) {
			return entity;
		}
		entity = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
				.eq("company_no", userInfo.getCompanyNo())
				.eq("page_type", PageCardTypeEnum.CardType_Com.getCode())
				.eq("rows_state", EnabledEnum.ENABLED.getCode()));
		if (entity != null) {
			return entity;
		}

		entity = pageCardConfigInfoService
				.getOne(new QueryWrapper<PageCardConfigInfo>()
						.eq("page_type", PageCardTypeEnum.CardType_PT.getCode())
						.eq("rows_state", EnabledEnum.ENABLED.getCode()));
		if (entity != null) {
			return entity;
		}
		return null;
	}

	@Override
	public PageCardConfigInfo addUserPageCardByCompany(Integer userId, String companyNo, LoginSimpleUserInfo userInfo) {
		PageCardConfigInfo userPage = pageCardConfigInfoService.getOne(
				new QueryWrapper<PageCardConfigInfo>()
						.eq("user_id",userId)
						.eq("rows_state", EnabledEnum.ENABLED.getCode())
						.eq("page_type", PageCardTypeEnum.CardType_Cust.getCode())
		);

		if(userPage!=null){
			return userPage;
		}
		PageCardConfigInfo companyPage = pageCardConfigInfoService.getOne(new QueryWrapper<PageCardConfigInfo>()
				.eq("company_no", companyNo)
				.eq("page_type", PageCardTypeEnum.CardType_Com.getCode())
				.eq("rows_state", EnabledEnum.ENABLED.getCode()));
		if (companyPage == null) {
			return null;
		}

		PageCardConfigInfo entity=new PageCardConfigInfo();
		entity.setPageNo(PageCachekeys.getPageNo());
		entity.setCompanyNo(companyNo);
		entity.setPageType(PageCardTypeEnum.CardType_Cust.getCode());
		entity.setUserId(userId);
		entity.setPageName(companyPage.getPageName());
		entity.setPageRemark(companyPage.getPageRemark());
		entity.setContent(companyPage.getContent());
		entity.setIsDraft(BaseConstant.YNEnum.NO.getCode());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		pageCardConfigInfoService.save(entity);
		return entity;
	}


}
